package com.ruoyi.common.utils.pinyin;

import net.sourceforge.pinyin4j.PinyinHelper;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

public class PinYinUtils {

    /**
     * 提取每个汉字的首字母(大写)
     *
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {
        if (isNull(str)) {
            return "";
        }
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取汉字的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }

        convert = string2AllTrim(convert);
        return convert.toLowerCase();
    }

    /*
     * 判断字符串是否为空
     */

    private static boolean isNull(Object strData) {
        if (strData == null || String.valueOf(strData).trim().equals("")) {
            return true;
        }
        return false;
    }

    /**
     * 去掉字符串包含的所有空格
     *
     * @param value
     * @return
     */
    private static String string2AllTrim(String value) {
        if (isNull(value)) {
            return "";
        }
        return value.trim().replace(" ", "");
    }

    /**
     * 获取编码非生产
     *
     * @Auth: levicash
     */
    public static String getCodeNormal(Integer maxNum, String type) {
        maxNum = maxNum + 1;
        String code = "";
        if (maxNum < 10) {
            code = "000" + maxNum.toString();
        } else if (maxNum >= 10 && maxNum < 100) {
            code = "00" + maxNum.toString();
        } else if (maxNum >= 100 && maxNum < 1000) {
            code = "0" + maxNum.toString();
        } else {
            code = maxNum.toString();
        }
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format2 = simpleDateFormat.format(date);
        return type + format2 + code;
    }
    public static String getProduce(Integer maxNum) {
        maxNum = maxNum + 1;
        String code = "";
        if (maxNum < 10) {
            code = "000" + maxNum.toString();
        } else if (maxNum >= 10 && maxNum < 100) {
            code = "00" + maxNum.toString();
        } else if (maxNum >= 100 && maxNum < 1000) {
            code = "0" + maxNum.toString();
        } else {
            code = maxNum.toString();
        }
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
        String format2 = simpleDateFormat.format(date);
        return format2 +'-'+ code;
    }

    public static String sequence(Integer maxNum) {
        maxNum = maxNum + 1;
        String code = "";
        if (maxNum < 10) {
            code = "000" + maxNum.toString();
        } else if (maxNum >= 10 && maxNum < 100) {
            code = "00" + maxNum.toString();
        }else if (maxNum >= 100 && maxNum < 1000) {
            code = "0" + maxNum.toString();
        } else {
            code = maxNum.toString();
        }
        return code;
    }
    public static String hundred(Integer maxNum) {
        String code = "";
        if (maxNum < 10) {
            code = "00" + maxNum.toString();
        } else if (maxNum >= 10 && maxNum < 100) {
            code = "0" + maxNum.toString();
        } else {
            code = maxNum.toString();
        }
        return code;
    }
}
